/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: user
 * Date: 2023-10-20
 * Time: 20:30
 */
import binarysearchtree.BinarySearchTree;
import demo1.HashBuck;
import demo1.HashBuck2;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author 12629
 * @Description：
 */
class Student {
    public String id;

    public Student(String id) {
        this.id = id;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return Objects.equals(id, student.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }
}
public class Test {

    public static void main(String[] args) {
        public List<String> topKFrequent(String[] words, int k) {
            //1、先统计单词出现的次数->存储到了map当中
            Map<String,Integer> map = new HashMap<>();
            for(String word : words) {
                if(map.get(word) == null) {
                    map.put(word,1);
                }else {
                    int val = map.get(word);
                    map.put(word,val+1);
                }
            }
            //2、遍历好统计好的Map，把每组数据存储到小根堆当中
            PriorityQueue<Map.Entry<String,Integer>> minHeap =
                    new PriorityQueue<>(new Comparator<Map.Entry<String, Integer>>() {
                        @Override
                        public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                            //放元素的时候 如果频率相同 我们转变为大根堆-》按照单词的字典序
                            if(o1.getValue().compareTo(o2.getValue()) == 0) {
                                return o2.getKey().compareTo(o1.getKey());
                            }
                            return o1.getValue().compareTo(o2.getValue());
                        }
                    });

            for(Map.Entry<String,Integer> entry : map.entrySet()) {
                if(minHeap.size() < k) {
                    minHeap.offer(entry);
                }else {
                    //你要找最大的频率的单词
                    Map.Entry<String,Integer> top = minHeap.peek();
                    if(top.getValue().compareTo(entry.getValue()) < 0) {
                        minHeap.poll();
                        minHeap.offer(entry);
                    }else {
                        //def->2                     abc-> 2
                        if(top.getValue().compareTo(entry.getValue()) == 0) {
                            if(top.getKey().compareTo(entry.getKey()) > 0) {
                                minHeap.poll();
                                minHeap.offer(entry);
                            }
                        }
                    }
                }
            }

            List<String> ret = new ArrayList<>();
            //放到了小根堆 2   3    4
            for (int i = 0; i < k; i++) {
                Map.Entry<String,Integer> top = minHeap.poll();
                ret.add(top.getKey());
            }
            // 2  3   4 ->  4 3 2
            Collections.reverse(ret);
            return ret;
        }








        public static void main12(String[] args) {
            int[] array = {1,2,3,3,2};
            Map<Integer,Integer> map = new HashMap<>();
            for(Integer x : array) {
                if(map.get(x) == null) {
                    //第一次存放
                    map.put(x,1);
                }else {
                    //其他情况在原来的基础上加 1
                    int val = map.get(x);
                    map.put(x,val+1);
                }
            }
            for(Map.Entry<Integer,Integer> entry : map.entrySet()) {
                System.out.println("key: "+entry.getKey()+" val: "+entry.getValue());
            }
        }

        public static void main10(String[] args) {
            int[] array = {1,2,3,3,2};
            HashSet<Integer> set = new HashSet<>();
            for (int i = 0; i < array.length; i++) {
                set.add(array[i]);
            }
            System.out.println(set);
        }


    /*public Node copyRandomList(Node head) {
        Map<Node,Node> map = new HashMap<>();
        Node cur = head;
        //1. 第一次遍历链表 存储对应关系
        while(cur != null) {
            Node node = new Node(cur.val);
            map.put(cur,node);
            cur = cur.next;
        }
        //2. 第2次遍历链表 开始修改每个节点的指向
        cur = head;
        while(cur != null) {
            map.get(cur).next = map.get(cur.next);
            map.get(cur).random = map.get(cur.random);
            cur = cur.next;
        }
        //3、返回head对应的地址
        return map.get(head);
    }*/

        public static void main9(String[] args) {
            Scanner in = new Scanner(System.in);
            // 注意 hasNext 和 hasNextLine 的区别
            while (in.hasNextLine()) { // 注意 while 处理多个 case
                String str1 = in.nextLine();
                String str2 = in.nextLine();
                func(str1,str2);
            }
        }

        private static void func(String str1,String str2) {
            HashSet<Character> set = new HashSet<>();
            for(char ch : str2.toUpperCase().toCharArray()) {
                set.add(ch);
            }
            HashSet<Character> set2 = new HashSet<>();
            for(char ch : str1.toUpperCase().toCharArray()) {
                if(!set.contains(ch) && !set2.contains(ch)) {
                    System.out.print(ch);
                    set2.add(ch);
                }
            }
        }
        public int numJewelsInStones(String jewels, String stones) {
            HashSet<Character> set = new HashSet<>();
            for(char ch : jewels.toCharArray()) {
                set.add(ch);
            }
            int count = 0;
            for(char ch : stones.toCharArray()) {
                if(set.contains(ch)) {
                    count++;
                }
            }
            return count;
        }
        public int singleNumber(int[] nums) {
            //HashSet<Integer> set = new HashSet<>();
            TreeSet<Integer> set = new TreeSet<>();
            for(int x : nums) {
                if(!set.contains(x)) {
                    set.add(x);
                }else {
                    set.remove(x);
                }
            }
            //集合当中 只有一个元素了

            for(int x : nums) {
                if(set.contains(x)) {
                    return x;
                }
            }
            return -1;
        }
        public static void main8(String[] args) {
            Student student1 = new Student("61012345");
            Student student2 = new Student("61012345");
            HashBuck2<Student,Integer> hashBuck2 = new HashBuck2<>();
            hashBuck2.put(student1,10);
            Integer v = hashBuck2.getValue(student2);
            System.out.println(v);
        }

        public static void main7(String[] args) {
            Student student1 = new Student("61012345");
            Student student2 = new Student("61012345");
            System.out.println(student1.hashCode());//x%len
            System.out.println(student2.hashCode());//x%len

            HashMap<Student,Integer> map2 = new HashMap<>();
            map2.put(student1,2);//hashcode
            /*map2.put(new Student("61012345"),2);*/
            System.out.println(map2.get(student2));
        }

        public static void main5(String[] args) {
            HashMap<String,Integer> map = new HashMap<>();
            map.put("hello",2);
            map.put("abcd",10);
            map.put("gao",3);


            Integer val = map.get("abcd");
            System.out.println(val);

            System.out.println(map);

            for(Map.Entry<String,Integer> entry :map.entrySet()){
                System.out.println("key: "+entry.getKey()+" val: "+entry.getValue());
            }
            //不支持迭代器遍历


        /*HashMap<Student,Integer> map2 = new HashMap<>();
        map2.put(new Student(),2);//hashcode
        map2.put(new Student(),2);
        map2.put(null,2);*/

            //不能存储相同的key  天然可以去重的  底层是一个HashMap 每次存储元素的时候 默认的value其实就是一个Objcet对象
            HashSet<String> set = new HashSet<>();
            set.add("hello");
            set.add("abcd");
            set.add("gaobo");

            System.out.println(set);


        }



        public static void main4(String[] args) {
            HashBuck hashBuck = new HashBuck();
            hashBuck.put(1,11);
            hashBuck.put(2,22);
            @@ -22,6 +267,8 @@ public class Test {
        hashBuck.put(21,2111);

        System.out.println(hashBuck.get(9));

                //ConcurrentHashMap
            }